//
//  XMUserTagsView.m
//  EnjoyShoppingStreet
//
//  Created by lifayu on 2017/9/6.
//  Copyright © 2017年 lifayu. All rights reserved.
//

#import "XMUserTagsView.h"
#import "XMUserTagCollectionViewCell.h"

@interface XMUserTagsView()<UICollectionViewDataSource,UICollectionViewDelegate,UICollectionViewDelegateFlowLayout>


@property (strong, nonatomic) UICollectionView *collectionView;


@end

@implementation XMUserTagsView

- (id)init
{
    self = [super init];
    if(self)
    {
        [self addSubview:self.collectionView];
        self.rows = 1;
        
    }
    return self;
}

- (id)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if(self)
    {
        [self addSubview:self.collectionView];
        self.rows = 1;
        
    }
    return self;
}

- (id)initWithCoder:(NSCoder *)aDecoder
{
    self = [super initWithCoder:aDecoder];
    if(self)
    {
        [self addSubview:self.collectionView];
        self.rows = 1;
    }
    return self;
}

- (UICollectionView *)collectionView
{
    if(!_collectionView)
    {
        FlyCollectionViewAlignFlowLayout *layout = [[FlyCollectionViewAlignFlowLayout alloc] initLayoutWithCollectionViewAlignType:FlyCollectionViewAlignLeft];
        _collectionView = [[UICollectionView alloc] initWithFrame:CGRectZero collectionViewLayout:layout];
        _collectionView.dataSource = self;
        _collectionView.delegate = self;
        _collectionView.bounces = NO;
        _collectionView.showsVerticalScrollIndicator = NO;
        _collectionView.showsHorizontalScrollIndicator = NO;
        _collectionView.backgroundColor = kClearColor;
        _collectionView.scrollEnabled = NO;
        [_collectionView registerNib:[UINib nibWithNibName:@"XMUserTagCollectionViewCell" bundle:nil] forCellWithReuseIdentifier:@"XMUserTagCollectionViewCell"];
        
        
    }
    return _collectionView;
}

- (void)setCollectionViewAlignType:(FlyCollectionViewAlignType)collectionViewAlignType
{
    _collectionViewAlignType = collectionViewAlignType;
    
    FlyCollectionViewAlignFlowLayout *layout = [[FlyCollectionViewAlignFlowLayout alloc] initLayoutWithCollectionViewAlignType:_collectionViewAlignType];
    layout.scrollDirection = UICollectionViewScrollDirectionVertical;
    
    [self.collectionView setCollectionViewLayout:layout];
    [self.collectionView reloadData];
    
}

- (void)setUserTags:(NSArray *)userTags
{
    if(_userTags != userTags)
    {
        _userTags = userTags;
        [self.collectionView reloadData];
    }
}

- (void)setRows:(NSUInteger)rows
{
    if(_rows != rows)
    {
        _rows = rows;
        [self.collectionView reloadData];
    }
}


- (void)layoutSubviews
{
    [super layoutSubviews];
    self.collectionView.frame = self.bounds;
    [self.collectionView reloadData];
}





#pragma mark - UICollectionViewDataSource/Delegate
- (NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView
{
    return 1;
}

- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section
{
    return self.userTags.count;
}

- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
    XMUserTagCollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"XMUserTagCollectionViewCell" forIndexPath:indexPath];
    NSString *tag = [self.userTags objectAtIndex:indexPath.item];
    [cell.tagButton setTitle:tag forState:UIControlStateNormal];
    if(self.isFriendsIndexTags)
    {
        cell.tagButton.backgroundColor = kClearColor;
        [cell.tagButton setTitleColor:UIColorFromRGBA(0x000000, 0.6) forState:UIControlStateNormal];
        cell.tagButton.borderWidth = 0.5;
        cell.tagButton.borderColor = UIColorFromRGBA(0x000000, 0.6);
    }
    return cell;
}


- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath
{
    NSString *tag = [self.userTags objectAtIndex:indexPath.item];
    
    CGSize size = [tag sizeWithFont:kSystemFontSize(14) maxSize:CGSizeMake(ScreenWidth, ScreenHeight)];
    if(_rows == 0)
    {
        return CGSizeMake(size.width + 8, size.height + 8);
    }
    else{
        return CGSizeMake(size.width + 8, size.height + 8);
    }
    
    
}

- (CGFloat)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout minimumLineSpacingForSectionAtIndex:(NSInteger)section
{
    return 2;
}

- (CGFloat)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout minimumInteritemSpacingForSectionAtIndex:(NSInteger)section
{
    return 2;
}


+ (CGFloat)tagRowHeight//每行的行高
{
    CGSize size = [@"每行的行高" sizeWithFont:kSystemFontSize(14) maxSize:CGSizeMake(ScreenWidth, ScreenHeight)];
    return size.height + 8;
}

/**
 根据tags 和 限制的行数 返回tagsView 的总高度
 
 @param tags tags
 @param row row
 @param maxWidth tagsView的宽度
 @return tagsView 的总高度
 */
+ (CGFloat)tagsViewContentHeight:(NSArray *)tags row:(NSUInteger)row maxWidth:(CGFloat)maxWidth//view content height
{
    NSInteger tagsRow = 1;
    CGFloat tagsRowWidht = 0;
    CGFloat tagsRowHeigth = [self tagRowHeight];
    
    for (NSUInteger index = 0; index < tags.count; index ++)
    {
        NSString *tag = tags[index];
        CGSize size = [tag sizeWithFont:kSystemFontSize(14) maxSize:CGSizeMake(ScreenWidth, ScreenHeight)];
        CGFloat itemWidth = size.width + 8;
        tagsRowWidht += itemWidth;
        tagsRowWidht += 2;//加上间隔
        if(tagsRowWidht > maxWidth && index != tags.count - 1)
        {
            tagsRow ++;
            tagsRowWidht = 0;
        }
    }
    if(row == 0)
    {
        return tagsRow*tagsRowHeigth + (tagsRow-1)*2;
    }
    else
    {
        if(row > tagsRow)
        {
            return tagsRow*tagsRowHeigth + (tagsRow-1)*2;
        }
        return row*tagsRowHeigth + (row-1)*2;
    }
}

@end
